<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('spreadArray')){
	function spreadArray($orig,$column){		
		$result = array();

		if(is_array($orig)){
			foreach($orig as $record){

				if(isset($record[$column])){
					$key = $record[$column];
				}else{
					$key = 0;
				}

				$result[$key][] = $record;
			}
		}

		return $result;
	}
}

if ( ! function_exists('extractColumn')){
	function extractColumn($list,$column){		
		$result = array();

		if(is_array($list)){
			foreach($list as $record){

				if(isset($record[$column])){
					$result[] = $record[$column];
				}
			}
		}
		$result = array_unique($result);

		return $result;
	}
}

if ( ! function_exists('reindexArray')){
	function reindexArray($orig,$column){		
		$result = array();

		if(is_array($orig)){
			foreach($orig as $record){

				if(isset($record[$column])){
					$key = $record[$column];
				}else{
					$key = 0;
				}

				$result[$key] = $record;
			}
		}

		return $result;
	}
}

if ( ! function_exists('sortArray')){
	function sortArray(&$arr,$column,$dir = SORT_ASC){//SORT_DESC
		$sortColumn = array();
		foreach ($arr as $key => $row) {
			$sortColumn[$key]  = $row[$column];
		}

		array_multisort($sortColumn,$dir,$arr);		
	}
}

if ( ! function_exists('id2name')){
	function id2name($id,$arr,$default = ''){
		if(isset($arr[$id])){
			return $arr[$id];
		}else{
			return $default;
		}
	}
}

if ( ! function_exists('array4js')){
	function array4js($orig){
		$arr = array();
		if ( array_keys($orig) !== range(0, count($orig) - 1)) {
			foreach ($orig as $key => $value) {
				$arr[] = array($key,$value);
			}
		} else {
			foreach ($orig as $value) {
				$arr[] = array($value,$value);
			}
		}
		
		return json_encode($arr);
	}
}

if ( ! function_exists('array_depth')){
	function array_depth($arr) {
	if (!is_array($arr)) { return 0; }
		$arr = json_encode($arr);

		$varsum = 0; $depth  = 0;
		for ($i=0;$i<strlen($arr);$i++)
		{
		$varsum += intval($arr[$i] == '[') - intval($arr[$i] == ']');
		if ($varsum > $depth) { $depth = $varsum; }
		}

		return $depth;
	}
}